package com.tjetc.Impl;

import com.tjetc.domain.Examination;
import com.tjetc.mapper.ExaminationMapper;
import com.tjetc.service.ExaminationService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

public class ExaminationServiceImpl implements ExaminationService {
    ExaminationMapper examinationMapper= Db.getMapper(ExaminationMapper.class);
    private Result result=new Result();

    @Override
    public Result addExamination(Examination examination) {
        Examination existing = examinationMapper.selectByPrimaryKey(examination.getNewbornId());
        if (existing != null) {
            result.setCode(400);
            result.setMsg(" 记录已存在,婴儿已经接受检查,如果需要更改请选择更改新生儿检查报告");
            return result;
        }
        int insert = examinationMapper.insert(examination);
        if (insert==1){
            result.setCode(200);
            result.setMsg("添加成功");
            return result;
        }
        result.setCode(500);
        result.setMsg("添加失败");
        return result;
    }

    @Override
    public Result updateExamination(Examination examination) {
        Examination existing = examinationMapper.selectByPrimaryKey(examination.getNewbornId());
        if (existing == null) {
            result.setCode(400);
            result.setMsg(" 输入的婴儿编号不存在");
            return result;
        }
        int update = examinationMapper.updateByPrimaryKeySelective(examination);
        if (update==1){
            result.setCode(200);
            result.setMsg("修改成功");
            return result;
        }
        result.setCode(500);
        result.setMsg("修改失败");
        return result;
    }

    @Override
    public Examination findExamination(int id) {
        Examination examination = examinationMapper.selectByPrimaryKey(id);
        if  (examination==null){
            System.out.println("暂时没有该新生儿的检测单,请先对新生儿进行检测");
            return null;
          }
        return examination;
    }
}
